Skip to main content

Sending mail to all users

Sending Email

Today we will see how to send email notifications to multiple users simultaneously. I'll walk you through the process of creating a Bash script that sends notifications to all logged-in users on a Linux system.

Step 1: Installing the Required Tool

Before we get started with the script, let's ensure that we have the necessary tools installed. We will use the mailutils package, which provides the mail command for sending emails. To install it, run the following command:

apt install mailutils

Step 2: Identifying Logged-In Users

Our script will send notifications to all users who are currently logged in. We can achieve this by using the who command along with some text processing. Here's how you can find all logged-in users:

who | cut -d' ' -f1 | sort -u

The output of this command will be a list of usernames, one per line. In this example, the output is 'ubuntu,' which represents the currently logged-in user.

Step 3: Writing the Bash Script

Now, let's create the Bash script that sends notifications. You can use your preferred text editor, like nano:

nano test.sh

Inside the script, define the message content, subject, sender email address, and a blind carbon copy (BCC) recipient. Here's the script:

#!/bin/bash

message="Your message content"
subject="Subject"
from="me@sapkotasunil.com"
bcc="sunil@gaintplay.com"

for user in $(who | cut -d' ' -f1 | sort -u); do
echo "$message" | mail -s "$subject" -r "$from" "$user"
done

# Send a BCC to a specified recipient
echo "$message" | mail -s "$subject" -r "$from" "$bcc"

This script sets up the message content, subject, and sender's email address. It then iterates through the list of logged-in users and sends each user an email with the defined content and subject. Finally, it sends a BCC email to the recipient specified in the bcc variable.

Step 4: Making the Script Executable

To make the script executable, run the following command:

chmod +x test.sh

Step 5: Running the Script

Execute the script to send the notifications to all logged-in users:

./test.sh

Step 6: Checking the Mail Log

To ensure that the emails were sent successfully, you can check the mail log for any relevant entries. On most Linux systems, the mail log is typically located at /var/log/mail.log. However, in your example, you encountered an issue while trying to navigate to this directory. Instead, you can directly view the contents of the mail log using the cat command:

cat /var/log/mail.log

The mail log will contain entries indicating the delivery status of the emails. For example, you may see entries like the following:

Nov  6 06:30:35 ip-172-31-94-205 postfix/pickup[2082105]: 76D984E0D8: uid=0 from=<me@sapkotasunil.com>
Nov 6 06:30:35 ip-172-31-94-205 postfix/local[2085728]: 76D984E0D8: to=<ubuntu@ip-172-31-94-205.ec2.internal>, orig_to=<ubuntu>, relay=local, delay=0.03, delays=0.02/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Nov 6 06:32:05 ip-172-31-94-205 postfix/smtp[2085730]: 7BE7A4E0D8: to=<sunil@gaintplay.com>, relay=none, delay=90, delays=0.01/0.02/90/0, dsn=4.4.1, status=deferred (connect to alt1.aspmx.l.google.com[209.85.202.27]:25: Connection timed out)

These entries indicate the successful delivery of the email to the local mailbox and a deferred delivery due to connection issues when sending to the BCC recipient.

Step 7: Checking Received Mail

To verify that the emails were sent, you can use the mail command to check your mailbox. Here's an example of how to use it:

mail

The output will show any received emails, including the content and sender information. For example:

"/var/mail/ubuntu": 1 message 1 new
>N 1 root Mon Nov 6 06:30 17/553 Subject
? 1
Return-Path: <me@sapkotasunil.com>
X-Original-To: ubuntu
Delivered-To: ubuntu@ip-172-31-94-205.ec2.internal
Received: by ip-172-31-94-205.ec2.internal (Postfix, from userid 0)
id 76D984E0D8; Mon, 6 Nov 2023 06:30:35 +0000 (UTC)
Subject: Subject
To: ubuntu@ip-172-31-94-205.ec2.internal
User-Agent: mail (GNU Mailutils 3.14)
Date: Mon, 6 Nov 2023 06:30:35 +0000
Message-Id: <20231106063035.76D984E0D8@ip-172-31-94-205.ec2.internal>
From: root <me@sapkotasunil.com>
X-IMAPbase: 1699252362 2
X-UID: 1
Status: R

Your message content

This output shows the received email with information about the sender, subject, and message content.

we've created a Bash script to automate email notifications to logged-in users on a Linux system. By following these steps, you can easily adapt the script to send notifications for various purposes, such as system alerts or important updates. Automation like this can simplify your daily tasks and improve your productivity.